package bishizhuanyong.欧科云链;

import java.util.Scanner;

public class Main {


    public static int maxProfit=Integer.MIN_VALUE;
    public static void main(String[] args) {
        Scanner sc=new Scanner(System.in);
        int n = sc.nextInt();
        int k = sc.nextInt();
        int[] prices=new int[n];
        for (int i=0;i<prices.length;i++)
            prices[i]=sc.nextInt();
        process(prices,k);
        System.out.println(maxProfit<=0?0:maxProfit);
    }

    public static void process(int[] prices,int k){
        rp_dp(prices,0,k,0,false);
    }

    public static void rp(int[] prices,int index,int k,int cur_profit,boolean pre_buy){
        if (index>=prices.length||k==0){
            maxProfit=Math.max(maxProfit,cur_profit);
            return;
        }
        if (pre_buy==false){
            //不买
            rp(prices,index+1,k,cur_profit,pre_buy);
            //买
            rp(prices,index+1,k,cur_profit-prices[index],true);
        }else {
            //不卖
            rp(prices,index+1,k,cur_profit,pre_buy);
            //卖
            rp(prices,index+1,k-1,cur_profit+prices[index],false);
        }
    }

    public static void rp_dp(int[] prices,int index,int k,int cur_profit,boolean pre_buy){
        if (prices.length==0)
            return;
        int[][][] dp=new int[prices.length][k+1][2];
        for (int i=0;i<=k;i++)
            dp[0][i][1]=-prices[0];
        for (int i=1;i<prices.length;i++){
            for (int j=k-1;j>=0;j--){
                dp[i][j][0]=Math.max(dp[i-1][j][0],dp[i-1][j][1]+prices[i]);
                dp[i][j][1]=Math.max(dp[i-1][j][1],dp[i-1][j+1][0]-prices[i]);
            }
        }
        maxProfit=Math.max(maxProfit,dp[prices.length-1][0][0]);
    }



}
